﻿using Microsoft.AspNetCore.Mvc;
using OpenAi.Util.Security;
using OpenAiWrapper.API.ResourceParameters;
using OpenAiWrapper.API.Services;

namespace OpenAiWrapper.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WrapperController : ControllerBase
    {
        private readonly IConfigService _configService;
        private readonly IOpenAiService _openAiService;

        public WrapperController(IConfigService configService, IOpenAiService openAiService)
        {
            _configService = configService;
            _openAiService = openAiService;
        }

        /// <summary>
        /// 获取聊天补全
        /// </summary>
        [HttpGet("chat")]
        public async Task<IActionResult> GetChatCompletionAsync(
            [FromQuery] GptChatResourceParameters resourceParameters
        )
        {
            if (string.IsNullOrWhiteSpace(resourceParameters.ApiKey))
            {
                return BadRequest("ApiKey requested.");
            }

            if (string.IsNullOrWhiteSpace(resourceParameters.Question))
            {
                return BadRequest("Question requested.");
            }

            try
            {
                //解密
                var apiUrl = Decrypt(resourceParameters.ApiUrl ?? string.Empty);
                var apiKey = Decrypt(resourceParameters.ApiKey);
                var question = Decrypt(resourceParameters.Question);
                var model = resourceParameters.Model ?? string.Empty;
                var ret = await _openAiService.GetChatCompletionAsync(apiKey, question, model, apiUrl);
                return Ok(Encrypt(ret));
            }
            catch (Exception e)
            {
                return Problem($"GetChatCompletion failed: {e.Message}.");
            }
        }

        /// <summary>
        /// 获取聊天补全
        /// </summary>
        [HttpGet("chat-test")]
        public async Task<IActionResult> GetChatCompletion2Async([FromQuery] string question)
        {
            try
            {
                //解密
                var apiUrl = "https://api.wlai.vip/v1";
                var apiKey = "sk-CJOs4cGFuNJzaALuG7OawIS6k1T1tY9pkJWUUTf40uxS9hZI";
                var model = "gpt-4o";
                var ret = await _openAiService.GetChatCompletionAsync(apiKey, question, model, apiUrl);
                return Ok(Encrypt(ret));
            }
            catch (Exception e)
            {
                return Problem($"GetChatCompletion failed: {e.Message}.");
            }
        }

        /// <summary>
        /// 获取文本补全
        /// </summary>
        [HttpGet("text")]
        public async Task<IActionResult> GetTextCompletionAsync()
        {
            await Task.CompletedTask;
            return Ok();
        }

        private string Encrypt(string value) =>
            SafeDataHelper.Encrypt(value, _configService.AesKey);

        private string Decrypt(string value) =>
            SafeDataHelper.Decrypt(value, _configService.AesKey);
    }
}
